package com.github.crush0.myblog.controller;
import com.alibaba.fastjson.JSONObject;
import com.github.crush0.myblog.Exception.ServiceException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

public abstract class BaseController {
    public static final int SUCCESS=1000;
    public static final int DEFAULT_FAIL = 10000;//未知错误
    @ResponseBody
    @ExceptionHandler({ServiceException.class})
    public JSONObject ExceptionHandler(HttpServletRequest request, HttpServletResponse response, Throwable ex){
        JSONObject retJson = new JSONObject();
        HashMap<String,Object> retMap = new HashMap<>();
        retMap.put("status",DEFAULT_FAIL);
        retMap.put("data",null);
        retMap.put("message",DEFAULT_FAIL+"-"+ex.getMessage());
        retJson.putAll(retMap);
        return retJson;
    }
}
